package com.qf.service.impl;

import com.qf.dao.TeacherDao;
import com.qf.dto.Teaching;
import com.qf.entity.Comment;
import com.qf.entity.Teacher;
import com.qf.entity.Transcript;
import com.qf.service.TeacherService;
import com.qf.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author qiankunquan
 */
@Service
public class TeacherServiceImpl implements TeacherService {
    @Autowired
    private TeacherDao teacherDao;

    @Override
    public R entering(String snumber, int courseId) {
        if (snumber != null && courseId != 0) {
            if (teacherDao.entering(courseId, snumber) > 0) {
                return new R(0, "success", null);
            } else {
                return new R(1, "fail", null);
            }
        } else {
            return new R(1, "fail", null);
        }
    }

    @Override
    public R giveScore(String snumber, int courseId, int score) {
        if (snumber != null && courseId != 0) {
            if (teacherDao.giveScore(snumber, courseId, score) > 0) {
                return new R(0, "success", null);
            } else {
                return new R(1, "fail", null);
            }
        } else {
            return new R(1, "fail", null);
        }
    }

    @Override
    public R selectOne(String tnumber) {
        List<Teaching> list = null;
        if (tnumber != null) {
            if (!teacherDao.selectTeach(tnumber).isEmpty()) {
                list = teacherDao.selectTeach(tnumber);
                return new R(0, "success", list);
            } else {
                return new R(1, "fail", null);
            }
        }
        return new R(1, "fail", null);
    }

    @Override
    public List<Comment> selectComment(String tnumber, int courseId) {
        List<Comment> list = null;
        if (tnumber != null) {
            if (!teacherDao.selectComment(tnumber, courseId).isEmpty()) {
                list = teacherDao.selectComment(tnumber, courseId);
            }
        }
        return list;
    }

    @Override
    public R selectTeacherInfo(int id) {
        return new R(0, "OK", teacherDao.selectTeacherInfo(id));
    }

    @Override
    public R selectInvigilation(String tnumber) {
        if (tnumber != null) {
            return new R(0, "success", teacherDao.selectInvigilation(tnumber));
        } else {
            return new R(1, "fail", null);
        }
    }

    @Override
    public R selectSchedule(String tnumber) {
        if (tnumber != null) {
            return new R(0, "success", teacherDao.selectSchedule(tnumber));
        } else {
            return new R(1, "fail", null);
        }
    }

    @Override
    public R openCloseSignIn(String tnumber, int courseId) {
        if (tnumber != null && courseId != 0) {

            String signIn = teacherDao.selectSignIn(tnumber, courseId);
//            此处使用 == 判断 始终走else分支
            if ("1".equals(signIn)) {
                return new R(0, "关闭成功", teacherDao.closeSignIn(tnumber, courseId));
            } else {
                return new R(0, "开启成功", teacherDao.openSignIn(tnumber, courseId));
            }
        } else {
            return new R(1, "fail", null);
        }
    }


    @Override
    public R selectTeacher(String status, String snumber) {
        return new R(0, "success",teacherDao.selectTeacher(status,snumber));
    }
}